package com.zxx.autohomenavi.page_json;

import android.content.Context;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.zxx.autohomenavi.R;
import com.zxx.autohomenavi.adapter.FragmentAdapter;
import com.zxx.autohomenavi.base.BaseActivity;
import com.zxx.autohomenavi.fragment.TitleFragment;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.CommonPagerTitleView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

/**
 * Json动画
 * @author zxx on 2021/8/18
 */
public abstract class LottieTabActivity extends BaseActivity {

    public abstract Map<String, String> getTabs();

    public abstract int getTabLayoutId();

    private ViewPager viewPager;
    private MagicIndicator indicator;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_lottie_tab;
    }

    @Override
    protected void initView() {
        viewPager = findViewById(R.id.view_pager);
        indicator = findViewById(R.id.magic_indicator);
    }

    @Override
    protected void onCreateEnd(Bundle savedInstanceState) {
        super.onCreateEnd(savedInstanceState);
        initViewPager();
    }

    private void initViewPager() {
        final Map<String, String> tabs = getTabs();
        List<Fragment> fragmentList = new ArrayList<>();
        final List<String> titles = new ArrayList<>();
        final List<String> images = new ArrayList<>();

        for (String key : tabs.keySet()) {
            Fragment fragment = new TitleFragment();
            Bundle bundle = new Bundle();
            bundle.putString("title", key);
            fragment.setArguments(bundle);
            fragmentList.add(fragment);

            titles.add(key);
            images.add(tabs.get(key));
            //Fragment fragment1 = TitleFragment.getInstance(key);
            //fragmentList.add(fragment1);
        }

        FragmentAdapter adapter = new FragmentAdapter(getSupportFragmentManager(), fragmentList, titles);
        viewPager.setAdapter(adapter);

        CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setAdjustMode(true);
        CommonNavigatorAdapter navigatorAdapter = new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return tabs.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                CommonPagerTitleView pagerTitleView = new CommonPagerTitleView(context);
                View customLayout = LayoutInflater.from(context).inflate(getTabLayoutId(), null, false);
                final TextView tabText = customLayout.findViewById(R.id.item_tab_text);
                final AnimationRadioView tabImage = customLayout.findViewById(R.id.item_tab_image);

                tabText.setText(titles.get(index));
                tabImage.setAnimation(images.get(index));

                pagerTitleView.setContentView(customLayout);

                pagerTitleView.setOnPagerTitleChangeListener(new CommonPagerTitleView.OnPagerTitleChangeListener() {
                    @Override
                    public void onSelected(int index, int totalCount) {
                        tabImage.setChecked(true);
                        tabText.setTypeface(Typeface.DEFAULT_BOLD);
                    }

                    @Override
                    public void onDeselected(int index, int totalCount) {
                        tabImage.setChecked(false);
                        tabText.setTypeface(Typeface.DEFAULT);
                    }

                    @Override
                    public void onLeave(int index, int totalCount, float leavePercent, boolean leftToRight) {

                    }

                    @Override
                    public void onEnter(int index, int totalCount, float enterPercent, boolean leftToRight) {

                    }
                });

                pagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        viewPager.setCurrentItem(index);
                    }
                });
                return pagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                return null;
            }
        };
        commonNavigator.setAdapter(navigatorAdapter);

        indicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(indicator, viewPager);
    }
}
